package com.sjzl.util;

public class Pagination {
	private int pageNo;//当前页码
	private int pageSize;//每页记录数
	private int totalCount;//总记录数
	private int pageCount;//总页数
	private int firstResult;//本页的第一条记录
	private String pageUrl;
	
	public Pagination(int pageNo, int pageSize, int totalCount, String pageUrl){
		this.pageNo = pageNo;
		this.pageSize = pageSize;
		this.totalCount = totalCount;
		this.pageUrl = pageUrl;
	}
	
	private void calculate() {
		pageCount = (totalCount + pageSize - 1) / pageSize;
		firstResult = (pageNo - 1) * pageSize;
	}
	
	public String toString(){
		calculate();
		
		StringBuffer buffer = new StringBuffer();
		
		buffer.append("<div class='pagenum'>");
		buffer.append(" 第" + pageNo + "/" + pageCount + "页  ");
		buffer.append("		");
		buffer.append("<a href='"+pageUrl+"&pageNo="+1+"'>首页</a>");
		buffer.append("		");
		buffer.append(pageCount == 0 || pageNo == 1 ? "<a style='cursor: pointer; color: red;'>上一页</a>" : "<a href='"+pageUrl+"&pageNo="+(pageNo-1)+"'>上一页</a>");
		buffer.append("		");
		for(int i=1;i<=pageCount;i++){
			buffer.append("<a href='"+pageUrl+"&pageNo="+i+"'>"+i+"</a>");
			buffer.append("		");
		}
		buffer.append("		");
		buffer.append(pageCount == 0 || pageNo == pageCount ? "<a style='cursor: pointer; color: red;'>下一页</a>" : "<a href='"+pageUrl+"&pageNo="+(pageNo+1)+"'>下一页</a>");
		buffer.append("		");
		buffer.append("<a href='"+pageUrl+"&pageNo="+pageCount+"'>尾页</a>");
		buffer.append("</div>");
		
		return buffer.toString();
	}
	
	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	
	public int getPageSize() {
		calculate();
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
		calculate();
	}
	
	public int getTotalCount() {
		calculate();
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
		calculate();
	}
	
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	
	public int getFirstResult() {
		calculate();
		return firstResult;
	}
	public void setFirstResult(int firstResult) {
		this.firstResult = firstResult;
		calculate();
	}
	
	public String getPageUrl() {
		return pageUrl;
	}
	public void setPageUrl(String pageUrl) {
		this.pageUrl = pageUrl;
	}
	
}
